// 大屏适配
<template>
  <div class="screen-adapter">
    <div class="content-wrap" :style="style">
      <slot />
    </div>
  </div>
</template>

<script>
export default {
  name: 'ScreenAdapter',
  data () {
    return {
      style: {
        width: `${this.w}px`,
        height: `${this.h}px`,
        transform: 'scale(1) translate(-50%, -50%)' // 默认不缩放，垂直水平居中
      }
    }
  },
  props: {
    w: {
      // 设计图尺寸宽
      type: Number,
      default: 1920
    },
    h: {
      // 设计图尺寸高
      type: Number,
      default: 1080
    }
  },
  mounted () {
    this.setScale()
    this.onresize = this.debounce(() => this.setScale(), 100)
    window.addEventListener('resize', this.onresize)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.onresize)
  },
  methods: {
    // 防抖
    debounce (fn, t) {
      const delay = t || 500
      let timer
      return function () {
        if (timer) {
          clearTimeout(timer)
        }
        const context = this
        timer = setTimeout(() => {
          timer = null
          fn.apply(context, arguments)
        }, delay)
      }
    },
    // 获取缩放比例
    getScale () {
      const w = window.innerWidth / this.w
      const h = window.innerHeight / this.h
      // return { w: w, h: h }
      return w < h ? w : h
    },
    // 设置缩放比例
    setScale () {
      const scale = this.getScale()
      // this.style.transform = `scale(${scale.w}, ${scale.h}) translate(-50%, -50%)`
      this.style.transform = `scale(${scale}) translate(-50%, -50%)`
    }
  }
}
</script>

<style lang="scss">
.screen-adapter {
  width: 100vw;
  min-height: 100%;
  max-height: 100vh;
  overflow: hidden;
  // background: url("../../assets/charts/icon-bg.png") no-repeat;
  background-size: 100% 100%;
  .content-wrap {
    // background-color: rgba(231, 245, 255, 1);
    transform-origin: 0 0;
    position: absolute;
    top: 50%;
    left: 50%;
    padding: 18px 20px;
    box-sizing: border-box;
    transform: translate(-50%, -50%);
    margin: 0 auto;
  }
}
</style>
